package com.six.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.six.business.dao.CheckBusinessMapper;
import com.six.business.vo.BusinessVo;
import com.six.core.entity.Business;
import com.six.business.service.BusinessService;
import com.six.business.dao.BusinessMapper;
import com.six.core.entity.CheckBusiness;
import com.six.core.util.JwtUtil;
import com.six.core.util.WebUtil;
import com.six.core.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 汪鹏
* @description 针对表【sto_business】的数据库操作Service实现
* @createDate 2023-02-27 14:26:44
*/
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService{
    @Autowired
    private BusinessMapper businessDao;

    @Autowired
    private CheckBusinessMapper checkBusinessDao;

    /** 前台商家中心申请入驻商家 */
    @Override
    public Result addBusiness(Business business) {

        CheckBusiness checkBusiness = new CheckBusiness();
        checkBusiness.setUserid(business.getUserid());

        //用户提交入驻前，先判断用户是否提交过审核，如果提交过，先删除之前提交的审核信息。
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",business.getUserid());
        List<Business> businesses = businessDao.selectList(queryWrapper);

        QueryWrapper<CheckBusiness> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("userid",business.getUserid());
        List<CheckBusiness> checkBusinesses = checkBusinessDao.selectList(queryWrapper1);

        if (businesses!=null && checkBusinesses!=null){
            int delete = businessDao.delete(queryWrapper);
            int delete1 = checkBusinessDao.delete(queryWrapper1);
        }

        businessDao.insert(business);
        checkBusinessDao.insert(checkBusiness);

        return new Result(200,"申请成功，待审核");
    }

    /** 前台商家中心获取商家信息 */
    @Override
    public Result getBusinessByUserId(Integer userId) {

        QueryWrapper<Business> businessQueryWrapper = new QueryWrapper<>();
        businessQueryWrapper.eq("userid",userId);
        Business business = businessDao.selectOne(businessQueryWrapper);

        return new Result(200,"查询成功",business);
    }

    /**  前台商家中心修改商家信息 */
    @Override
    public Result updateBusiness(Business business) {
        UpdateWrapper<Business> wrapper = new UpdateWrapper<>();
        wrapper.set("realname",business.getRealname())
                .set("license",business.getLicense())
                .set("cardfront",business.getCardfront())
                .set("cardreverse",business.getCardreverse())
                .set("idcard",business.getIdcard());
        wrapper.eq("userid",business.getUserid());
        int i = businessDao.update(new Business(), wrapper);
        return new Result(200,"修改成功");
    }

    @Override
    public Result findApproval(int currentPage, int pageSize, BusinessVo businessVo) {
        //设置分页的页码以及分页显示的条数---必须在查询前设置
        PageHelper.startPage(currentPage,pageSize);
        List<Business> businessList = businessDao.findApproval(businessVo);
        //把查询的集合结果封装到PageInfo中 方便得到更多的数据值
        PageInfo<Business> PageInfo = new PageInfo<>(businessList);
        return new Result(200,"查询成功",PageInfo);
    }

    @Override
    public Result findUnreviewed(int currentPage, int pageSize, BusinessVo businessVo){
        //设置分页的页码以及分页显示的条数---必须在查询前设置
        PageHelper.startPage(currentPage,pageSize);
        List<Business> businessList = businessDao.findUnreviewed(businessVo);
        //把查询的集合结果封装到PageInfo中 方便得到更多的数据值
        PageInfo<Business> PageInfo = new PageInfo<>(businessList);
        return new Result(200,"查询成功1",PageInfo);
    }

}




